package ttOrder.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ttOrder.model.po.User;
import ttOrder.model.vo.UserVO;
import ttOrder.service.UserServiceImpl;
import ttOrder.util.CommonMessageResultVO;

@Controller
@RequestMapping("/ttOrder")
public class userController {
	
	@Resource
	private UserServiceImpl userService;
	
	@ResponseBody
	@RequestMapping("/userlist")
	public List<UserVO> userlist(Model model){
		List<UserVO> users = userService.getAllVO();
		model.addAttribute("users", users);
		return users;
	}
	
	@ResponseBody 
	@RequestMapping(value = "/addUser", method = RequestMethod.POST)
	public CommonMessageResultVO addUser(Model model,User user,String birthdayString){
		CommonMessageResultVO vo = new CommonMessageResultVO();
		try{
			//填入生日Date
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			java.util.Date birthday = sdf.parse(birthdayString);  
			user.setBirthday(birthday);
			//填入当前时间作为最后登录时间段
			Date currentTime = new Date();
			user.setLastLockTime(currentTime);
			
			userService.insert(user);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		vo.setSuccess(true);
		return vo;
	}
	
	
	@ResponseBody 
	@RequestMapping(value = "/editUser", method = RequestMethod.POST)
	public CommonMessageResultVO editUser(Model model,User user,String birthdayString){
		CommonMessageResultVO vo = new CommonMessageResultVO();
		try{
			System.out.println();
			//填入生日Date
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			java.util.Date birthday = sdf.parse(birthdayString);  
			user.setBirthday(birthday);
			//填入当前时间作为最后登录时间段
			Date currentTime = new Date();
			user.setLastLockTime(currentTime);
			
			userService.update(user);
		}catch(Exception e){
			e.printStackTrace();
		}
		vo.setSuccess(true);
		return vo;
	}
	
	@ResponseBody 
	@RequestMapping(value = "/delUser", method = RequestMethod.POST)
	public CommonMessageResultVO delFood(Model model,String[] ids){
		CommonMessageResultVO vo = new CommonMessageResultVO();
		try{
			for(int i = 0 ; i<ids.length ; i++){
				User user = new User();
				user.setId(ids[i]);
				userService.deleteByKey(user);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		vo.setSuccess(true);
		return vo;
	}
	
	@ResponseBody 
	@RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
	public CommonMessageResultVO updatePassword(Model model,String password,String id){
		CommonMessageResultVO vo = new CommonMessageResultVO();
		try{
				User key = new User();
				key.setId(id);
				User user = userService.getByKey(key);
				user.setPassword(password);
				userService.update(user);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		vo.setSuccess(true);
		return vo;
	}
}
